//
//  FashionViewController.m
//  kr_36
//
//  Created by lmt on 16/1/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "FashionViewController.h"
#import "LMTAFNetwork.h"
#import "RecommendModel.h"
#import "RecommendTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "LMTTImeChuo.h"
#import "RecommendDetailController.h"
#import <MJRefresh.h>

@interface FashionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;/**< 数据 */

@property (nonatomic, assign) NSInteger i;/**< 记录加载 */

@end

@implementation FashionViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"推荐";
    
    _i = 3;
    
    // 创建tableView
    [self createTableView];
    
    // 数据解析
    [self dataHander];
    
    // 下拉刷新
    [self refresh];
    
    // 上拉加载
    [self loading];

}

#pragma mark - 上拉加载
- (void)loading
{
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        //延迟两秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            NSString *str = [NSString stringWithFormat:@"http://app.api.huxiu.com/article/get_article_list?TIMESTAMP=1453799363&NONCE=huxiu_android_app&SIGNATURE=2f6d6730ee4f0c9464899842b80fae5f38914a18&TOKEN=&os=android&version=3.0&platform=SM-G9250-5.1.1&page=2&catid=%ld&", _i];
            
            [LMTAFNetwork postUrlString:str body:nil response:LMTJSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                
                NSArray *arr = responseObject[@"data"][@"datalist"];
                NSMutableArray *temp = [RecommendModel modelHanderWithArray:arr];
                
                for (RecommendModel *m in temp) {
                    [self.dataArr addObject:m];
                }
                [_tableView reloadData];
                _i++;
                
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                
            }];
            
            [_tableView.mj_footer endRefreshing];
        });
    }];
    _tableView.mj_footer = footer;
}

#pragma mark - 下拉刷新
- (void)refresh
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),^{
                           
                           // 刷新
                           [self dataHander];
                           //新版本是 MJ_header
                           [self.tableView.mj_header endRefreshing];
                       });
    }];
    
    self.tableView.mj_header = header;
    header.stateLabel.font = [UIFont systemFontOfSize:12];
}



#pragma mark - 数据解析
- (void)dataHander
{
    NSString *url = @"http://app.api.huxiu.com/article/get_article_list?TIMESTAMP=1453799363&NONCE=huxiu_android_app&SIGNATURE=2f6d6730ee4f0c9464899842b80fae5f38914a18&TOKEN=&os=android&version=3.0&platform=SM-G9250-5.1.1&page=2&catid=2&";
    //NSString *body = @"TIMESTAMP=1453769510&NONCE=huxiu_android_app&SIGNATURE=0c7fdf90582a2e555ab08ee0000dbaa75b650eba&TOKEN=&os=android&version=3.0&platform=SM-G9250-5.1.1&page=0&";
    
    [LMTAFNetwork postUrlString:url body:nil response:LMTJSON bodyStyle:(RequestJSON) requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"datalist"];
        self.dataArr = [RecommendModel modelHanderWithArray:arr];
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    RecommendModel *m = _dataArr[indexPath.row];
    
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.pic_path]];
    cell.title.text = m.title;
    id result = [NSNumber numberWithFloat:m.dateline.floatValue];
    NSString *str = [LMTTImeChuo boutiqueCurrentTime:result];
    cell.name.text = [NSString stringWithFormat:@"%@ / %@", m.author, str];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 250;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendDetailController *rdVC = [[RecommendDetailController alloc] init];
    
    RecommendModel *m = _dataArr[indexPath.row];
    
    rdVC.str = m.h5_url;
    
    [self.navigationController pushViewController:rdVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
